package cn.edu.neusoft.cqy917.zyfypt_cqy917.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.R;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.adapter.TwareAdapter;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.TwareBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.TwareListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.model.TwareModel;

import static android.content.Context.MODE_PRIVATE;



public class Fragment2 extends Fragment {
    private View view=null;
    private Context context;
    private int page=1;
    private int lastVisibleItemPosition;
    RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    TwareAdapter myAdapter;
    SharedPreferences sharedPreferences;
    String sessionID = "";
    List<TwareBean> list = null;
    TwareListener twareListener = new TwareListener() {
        @Override
        public void onResponse(List<TwareBean> blist) {
            if (page==1){
                list=blist;
            }
            else {
                list.removeAll(blist);
                list.addAll(blist);
            }
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment2,container,false);
        sharedPreferences = context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();


        recyclerView = (RecyclerView)view.findViewById(R.id.r2);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        myAdapter = new TwareAdapter(context);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);

        //以下为新增内容
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    TwareModel twareModel = new TwareModel();
                    twareModel.getResultList("tware",page,sessionID, twareListener);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });
        TwareModel twareModel = new TwareModel();
        twareModel.getResultList("tware",page,sessionID, twareListener);
        return view;
    }
    private void readSP(){
        sessionID = sharedPreferences.getString("sessionID",null);}



}


